package com.duoduo.mall.controller;

import java.util.Arrays;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.duoduo.mall.dto.LoginDTO;
import com.duoduo.mall.dto.PasswordUpdateDTO;
import com.duoduo.mall.dto.RegisterDTO;
import com.duoduo.mall.dto.UserInfoDTO;
import com.duoduo.mall.dto.UserUpdateDTO;
import com.duoduo.mall.entity.CategoryEntity;
import com.duoduo.mall.entity.UserEntity;
import com.duoduo.mall.service.UserService;
import com.duoduo.mall.utils.PageUtils;
import com.duoduo.mall.utils.R;
import com.duoduo.mall.vo.LoginVO;
import com.duoduo.mall.vo.UserVO;

import javax.validation.Valid;

/**
 * 用户表
 *
 * @author zhuge
 * @email 1399671339@qq.com
 * @date 2024-12-26 13:10:05
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("mall/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public R register(@RequestBody @Valid RegisterDTO registerDTO) {
        userService.register(registerDTO);
        return R.ok();
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R login(@RequestBody @Valid LoginDTO loginDTO) {
        LoginVO loginVO = userService.login(loginDTO);
        return R.ok().put("data", loginVO);
    }

    /**
     * 获取当前用户信息
     */
    @ApiOperation("获取个人信息")
    @GetMapping("/info")
    public R getCurrentUserInfo() {
        UserVO userInfo = userService.getCurrentUserInfo();
        return R.ok().put("data", userInfo);
    }


    /**
     * 更新当前用户信息--更新个人信息
     */
    @ApiOperation("更新个人信息")
    @PutMapping("/update")
    public R updateUserInfo(@RequestBody @Valid UserUpdateDTO userUpdateDTO) {
        userService.updateCurrentUserInfo(userUpdateDTO);
        return R.ok();
    }

    @ApiOperation("修改密码")
    @PutMapping("/password")
    public R updatePassword(@RequestBody @Valid PasswordUpdateDTO passwordUpdateDTO) {
        userService.updatePassword(passwordUpdateDTO);
        return R.ok();
    }

    @ApiOperation("管理员 - 查看用户列表")
    @GetMapping("/list")
    public R list(@RequestParam(required = false) Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);
        return R.ok().put("page", page);
    }

    @ApiOperation("管理员 - 根据id查询用户信息")
    @GetMapping("/info/id")
    public R getUserInfo(@RequestParam Long id) {
        UserVO userVO = userService.getUserInfoById(id);
        return R.ok().put("data", userVO);
    }

    @ApiOperation("管理员 - 删除/批量删除用户")
    @DeleteMapping("/delete")
    public R deleteUsers(@RequestParam("ids") Long[] ids) {
        userService.deleteUsersByIds(Arrays.asList(ids));
        return R.ok();
    }
}
